package com.itheima.controller;

import com.itheima.domain.Department;
import com.itheima.domain.Patient;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiuJie
 * 我亦无他,唯手熟尔
 */

@RestController
@RequestMapping
public class UserController {

    /*
    public Object save(@RequestBody Book book) {
        boolean flag = bookService.save(book);
        Integer code = flag ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = flag? "保存成功" : "保存失败";
        return new Result(code,flag,msg);
    }
     */
    @Autowired
    UserService service;

    @GetMapping("/department")
    public Object queryDepartment(){
        List<Department> list = service.queryDepartment();
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        System.out.println("code = " + code);
        String msg = list != null ? "查询科室成功" : "查询科室失败";
        System.out.println("msg = " + msg);
        return new Result(code, list, msg);
    }

    @PostMapping("/patient")
    public Object addPatient(@RequestBody Patient patient){
        Boolean flag = service.addPatient(patient);
        Integer code = flag ? Code.SAVE_OK : Code.SAVE_ERR;
        System.out.println("code = " + code);
        String msg = flag ? "新增病人成功" : "新增病人失败";
        System.out.println("msg = " + msg);
        return new Result(code, flag, msg);
    }

    @GetMapping("/patient/{pageNum}/{pageSize}/{patientName}")
    public Object queryPatientByName(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String patientName){
        pageNum = (pageNum - 1) * pageSize;
        System.out.println("pageNum = " + pageNum);
        System.out.println("pageSize = " + pageSize);
        System.out.println("patientName = " + patientName);
        PageResult pageResult = service.queryPatientByName(pageNum, pageSize, patientName);
        Integer code = pageResult.getRows() != null ? Code.GET_OK : Code.GET_ERR;
        System.out.println("code = " + code);
        String msg = pageResult.getRows() != null ? "分页查询病人成功" : "分页查询病人失败";
        System.out.println("msg = " + msg);
        return new Result(code, pageResult, msg);
    }

    @GetMapping("/patient/{pageNum}/{pageSize}")
    public Object queryPatientByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        pageNum = (pageNum - 1) * pageSize;
        System.out.println("pageNum = " + pageNum);
        System.out.println("pageSize = " + pageSize);
        PageResult pageResult = service.queryPatientByPage(pageNum, pageSize);
        Integer code = pageResult.getRows() != null ? Code.GET_OK : Code.GET_ERR;
        System.out.println("code = " + code);
        String msg = pageResult.getRows() != null ? "分页查询病人成功" : "分页查询病人失败";
        System.out.println("msg = " + msg);
        return new Result(code, pageResult, msg);
    }




}
